package com.wanxi.education.util;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/**
 * @Author: WangRui
 * @Description:
 * @Date: Created in 2021/9/3 0003
 * @Version: 1.0
 */

public class QiniuCloudUtil {

    private static Properties properties = new Properties();
    // 设置七牛云账号的AK和SK
    private static String ACCESS_KEY = null;
    private static String SECRET_KEY = null;
    // 要上传的空间bucket
    private static String BUCKET = null;
    //外链地址：这里要改成自己的域名
    private static String DOMAIN = null;

    private static SimpleDateFormat simpleDateFormat;

    private static int i = 1;

    static {
        try {
            properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("qiniuyun.properties"));
            ACCESS_KEY = properties.getProperty("accessKey");
            SECRET_KEY = properties.getProperty("secretKey");

            BUCKET = properties.getProperty("spaceName");
            DOMAIN = properties.getProperty("domainName");
            simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 获取七牛云Token
     */
    public static String getupToken() {
        //秘钥配置上传的凭证
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        StringMap putPolicy = new StringMap();
        putPolicy.put("returnBody", "{\"fileUrl\": \"" + DOMAIN + "$(key)\"}");//上传成功后是返回这个json数据，fileUrl就是的地址
        long expireSeconds = 3600;//过期时间

        String upToken = auth.uploadToken(BUCKET, null, expireSeconds, putPolicy);
        System.out.println(upToken);
        return upToken;
    }

    /**
     * @param
     * @return 外键地址
     */
    //普通上传
    public static String uploadFile() {
        //文件的外链地址
        StringBuffer fileUrl = new StringBuffer(DOMAIN);

        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone2());

        UploadManager uploadManager = new UploadManager(cfg);

        //如果是Windows情况下，格式是 D:\\qiniu\\test.png
        String localFilePath = "/Users/leeyunt/Downloads/1864602-3d5d1de100e459b8.png";
        //默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = null;

        try {
            //上传文件
            Response response = uploadManager.put(localFilePath, key, getupToken());

            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
            fileUrl.append(putRet.key);
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
        return fileUrl.toString();
    }

    //base64方式上传
    /*public static String uploadFileBase64(byte[] base64, String key) throws Exception {
        String file64 = Base64.encodeToString(base64, 0);
        Integer l = base64.length;
        String uploadUrl = "http://up-z2.qiniup.com/putb64/" + l + "/key/" + UrlSafeBase64.encodeToString(key);
        //非华南空间需要修改上传域名
        //上传文件
        RequestBody rb = RequestBody.create(null, file64);
        Request request = new Request.Builder().
                url(uploadUrl)
                .addHeader("Content-Type", "application/octet-stream")
                .addHeader("Authorization", "UpToken " + getupToken())
                .post(rb).build();
        //System.out.println(request.headers());
        OkHttpClient client = new OkHttpClient();
        okhttp3.Response response = client.newCall(request).execute();
        System.out.println(response);
        return DOMAIN + key;
    }*/

    //字节数组上传

    /**
     * 字节上传
     *
     * @param uploadBytes
     * @param fileName
     * @param spaceFile
     * @return 链接地址
     * @throws Exception
     */
    public static String uploadFileBytes(byte[] uploadBytes, String fileName, String spaceFile) throws Exception {
        //文件的外链地址
//        StringBuffer fileUrl = new StringBuffer(DOMAIN);
        String uri = "";

        //构造一个带指定Zone对象的配置类

        Configuration cfg = new Configuration(Region.autoRegion());

        UploadManager uploadManager = new UploadManager(cfg);
        //默认不指定key的情况下，以文件内容的hash值作为文件名
        //System.out.println("截取文件："+fileName.substring(fileName.indexOf(".")));

        String newFileName = simpleDateFormat.format(new Date())+ i + fileName.substring(fileName.indexOf("."));
        String key = spaceFile + "/" + newFileName;

        i++;
        //秘钥配置上传的凭证
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        String upToken = auth.uploadToken(BUCKET);

        try {
            //上传文件
            Response response = uploadManager.put(uploadBytes, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
//            System.out.println(putRet.key);
//            System.out.println(putRet.hash);
            uri = putRet.key;
//            fileUrl.append("/" + putRet.key);
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
//        return fileUrl.toString();
        return uri;
    }
}
